﻿var creditCardSelect = false;
var orderId = "";
var amountText = 0;
$(document).ready(function () {    
	//Generate random order id
	orderId = 1 + Math.floor(Math.random() * 100000);
	$("#_orderId").val(orderId);
	$("#orderNumberText").text(orderId);
	//Generate amount
	amountText = Math.floor((Math.random() * 10) + 1); 
	console.log(amountText);
	$("#amountTextOrder").text("$"+amountText+".00");
	$("#amountChargeText").text("$"+amountText+".00");
	$("#amountChargeSub").text("$"+amountText+".00");
	$("#_chargeTotal").text(amountText);
	$("#_subTotal").text(amountText);
    //Detect checked for radio button payment method
    detectPaymentMethod();
    //Detect checked for the term condition
    detectTermConditionChecked();
    //Handle radio payment method click
    $(".paymentMethod").click(function () {
        if ($(this).val()==1) {
            //Purchase Order
        	$("#submitOrder").val("Submit Order");
            $("#creditCardInfo").hide();
            $("#purchaseOrder").show();
            $("#transactionDate").show();
            $("#labelForPayByOrder").attr("class", "bold-and-italic");
            $("#labelForPayByCreditCard").attr("class", "inactive bold-and-italic");
        } else {
            //Credit card        	
        	$("#submitOrder").val("NEXT");
            $("#creditCardInfo").show();
            $("#purchaseOrder").hide();
            $("#transactionDate").hide();
            $("#labelForPayByOrder").attr("class", "inactive bold-and-italic");
            $("#labelForPayByCreditCard").attr("class", "bold-and-italic");
        }
    });
    // Handle checkbox term condition
    $("#chkAcceptTermCondition").click(function () {
        if ($(this).prop('checked') == true) {
            $("#submitOrder").attr("class", "mce");
            $("#submitOrder").removeAttr("disabled");
        } else {
            $("#submitOrder").attr("class", "mce disable");
            $("#submitOrder").attr("disabled", "disabled");
        }
    });
    //Handle button submitOrder
    $("#submitOrder").click(function(){    	
    	 var paymentMethod = $('input[name=paymentMethod]:checked', '#frmFinishedOrder').val();
    	 console.log("paymentMethod="+paymentMethod);
    	if(paymentMethod==2){
    		//Pay by visa card
    		var conceptCard = $('#conceptCards').find(":selected").val();
    		$("#_paymentMethod").val(conceptCard);
    		$("#frmSubmitOrder").submit();
    	}else{
    		window.location = $("#contextRoot").val()+'/create-order/order-completed';
    	}
    });
});

function detectPaymentMethod() {
    var paymentMethod = $('input[name=paymentMethod]:checked', '#frmFinishedOrder').val();
    if (paymentMethod == 1) {
        //Purchase Order
        $("#creditCardInfo").hide();
        $("#labelForPayByOrder").attr("class", "bold-and-italic");
        $("#labelForPayByCreditCard").attr("class", "inactive bold-and-italic");
    } else {
        //Credit card
        $("#creditCardInfo").show();
        $("#labelForPayByOrder").attr("class", "inactive bold-and-italic");
        $("#labelForPayByCreditCard").attr("class", "bold-and-italic");
        creditCardSelect=true;
    }
}
function detectTermConditionChecked() {
    if ($("#chkAcceptTermCondition").prop('checked') == true) {
        $("#submitOrder").attr("class", "mce");
        $("#submitOrder").removeAttr("disabled");
    } else {
        $("#submitOrder").attr("class", "mce disable");
        $("#submitOrder").attr("disabled", "disabled");
    }
}